package com.fowo.api.service.handleWork;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.ListingFather;
import com.fowo.api.entity.Product;
import com.fowo.api.mapper.ProductMapper;
import com.fowo.api.model.fbainvoice.FBAInvoicePushStockOutAction;
import com.fowo.api.model.fbainvoice.FBAInvoiceVo;
import com.fowo.api.model.fbainvoice.detail.FBAInvoiceDetailItemVo;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import com.fowo.api.service.FBAInvoiceService;
import com.fowo.api.service.StockOutService;
import com.fowo.api.service.impl.ListingFatherServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class FBAInvoicePushStockOut implements FBAInvoicePushStockOutAction {

    @Resource
    private FBAInvoiceService fbaInvoiceService;
    @Resource
    private StockOutService stockOutService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ListingFatherServiceImpl listingFatherServiceImpl;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(Long id : idList){
            FBAInvoiceVo fbaInvoiceVo = fbaInvoiceService.getVoById(id);
            if(null != fbaInvoiceVo.getIsOut() && fbaInvoiceVo.getIsOut()){
                throw new Exception("单号："+fbaInvoiceVo.getInoviceCode()+"已推送");
            }
            stockOutService.create(buildStockOut(fbaInvoiceVo));
            fbaInvoiceVo.setIsOut(true);
            fbaInvoiceService.update(fbaInvoiceVo);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public void runV2(List<String> idList) throws Exception {
        for(String id : idList){
            FBAInvoiceVo fbaInvoiceVo = fbaInvoiceService.getVoById(Convert.toLong(id));
            if(null != fbaInvoiceVo.getIsOut() && fbaInvoiceVo.getIsOut()){
                throw new Exception("单号："+fbaInvoiceVo.getInoviceCode()+"已推送");
            }
            stockOutService.create(buildStockOut(fbaInvoiceVo));
            fbaInvoiceVo.setIsOut(true);
            fbaInvoiceService.update(fbaInvoiceVo);
        }
    }

    public StockOutVo buildStockOut(FBAInvoiceVo fbaInvoiceVo){
        StockOutVo model = new StockOutVo();
        model.setWarehouse(fbaInvoiceVo.getDeliveryWarehouse());
        model.setType("CK003");
        model.setOutTime(fbaInvoiceVo.getSendTime());
//        model.setDept();
        model.setProductType("1");
        model.setOutTime(new Date());
        model.setStatus("CS02");
        model.setRemark("发货单号："+fbaInvoiceVo.getInoviceCode());
//        model.setNeedOrg();

        List<StockOutProductItemVo> stockOutProducts = new ArrayList<>();
        StockOutProductItemVo stockOutProductItemVo;
        for(FBAInvoiceDetailItemVo itemVo : fbaInvoiceVo.getInvoiceDetailList()){
            Product product = productMapper.selectById(itemVo.getName());
            Assert.notNull(product,"SKU："+product.getSku()+"不存在，请检查");
            stockOutProductItemVo = new StockOutProductItemVo();
            stockOutProductItemVo.setProductId(itemVo.getName());
            stockOutProductItemVo.setSku(itemVo.getSku());
            //发货数量
            stockOutProductItemVo.setSid(itemVo.getShipmentShop()!=null?Long.valueOf(itemVo.getShipmentShop()):null);

           if(itemVo.getShipmentShopFnsku()!=null&&!itemVo.getShipmentShopFnsku().equals("")){
               List<ListingFather> list = listingFatherServiceImpl.list(Wrappers.<ListingFather>query().lambda().eq(ListingFather::getFnsku, itemVo.getShipmentShopFnsku()));
               if(list.size()>0){
                   ListingFather listingFather = list.get(0);
                   stockOutProductItemVo.setFnsku(String.valueOf(listingFather.getId()));
                   stockOutProductItemVo.setFnskuId(listingFather.getId());
                   stockOutProductItemVo.setFnskuIdFnsku(itemVo.getShipmentShopFnsku());
               }
            }


            stockOutProductItemVo.setStorageBinGoodId(itemVo.getOutBingId());
            stockOutProductItemVo.setProductGoodNum(itemVo.getSendQuantity());
//            stockOutProductItemVo.setRejectsOperational(0);
//            stockOutProductItemVo.setStorageBinBadId();
            stockOutProductItemVo.setProductBadNum(0);
            stockOutProductItemVo.setQuantity(itemVo.getSendQuantity());
            BigDecimal procurementCost = product.getProcurementCost() == null ? new BigDecimal(0.00) : product.getProcurementCost();
            stockOutProductItemVo.setPrice(procurementCost);
            stockOutProductItemVo.setSubtotal(BigDecimal.valueOf(itemVo.getSendQuantity()).multiply(procurementCost));
//            stockOutProductItemVo.setCurrentIssueCostAmount();
//            stockOutProductItemVo.setTotal();
//            stockOutProductItemVo.setMainUnitPrice();
//            stockOutProductItemVo.setGramWeight();
            stockOutProducts.add(stockOutProductItemVo);
        }
        model.setStockOutProducts(stockOutProducts);
        return model;
    }
}
